package cn.gl.system.controller;

import cn.gl.basic.util.AjaxResult;
import cn.gl.basic.util.PageList;
import cn.gl.system.domain.Dictionarytype;
import cn.gl.system.query.DictionarytypeQuery;
import cn.gl.system.service.IDictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarytype")
@ResponseBody
@Api(value = "部门接口类",description ="处理部门的CRUD操作" )
class DictionarytypeController {
    @Autowired
    private IDictionarytypeService dictionarytypeService;

    /**
     * 单个查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Dictionarytype findOne(@PathVariable("id")Long id){
        return dictionarytypeService.laodOne(id);
    }

    /**
     * 多个查询
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询多个对象")
    public List<Dictionarytype> findAll(){
        return dictionarytypeService.laodAll();
    }

    /**
     * 分页与高级查询
     * @param dictionarytypeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页高级查询")
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery dictionarytypeQuery){
        return dictionarytypeService.queryPage(dictionarytypeQuery);
    }
    /**
     * 接口：删除
     * @param id
     * @return
     */
    //前端：delete http://localhost:8080/dictionarytype/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            dictionarytypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");

        }
    }

    /**
     * 添加或修改
     * @param dictionarytype
     * @return
     */
    //前端：put http://localhost:8080/dictionarytype/ json对象{}
    @PutMapping
    @ApiOperation(value = "添加和修改")
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype){
        try {
            if(dictionarytype.getId()==null){
                System.out.println(dictionarytype);
                dictionarytypeService.insert(dictionarytype);
            }else{
                dictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    @ApiOperation(value = "批量删除接口")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            //调用service
            dictionarytypeService.patchDelete(ids);
            return new AjaxResult();

        }catch(Exception e){
            e.printStackTrace();
            //return new AjaxResult(false,"删除失败");
            return  AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }
}